package com.mac.intellect_flow.reply;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Schema(name = "分页结果")
@Data
public final class PageResult<T> extends PageParam implements Serializable {
    private static final long serialVersionUID = 1L;

    @Schema(description = "数据", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<T> list;

    @Schema(description = "总量", requiredMode = Schema.RequiredMode.REQUIRED)
    private long total;

    @Schema(description = "总页数")
    private long totalPages;

    public PageResult() {
    }

    public PageResult(List<T> list, Long total) {
        this.list = list;
        this.total = total;
    }

    public PageResult(List<T> list, Long total, Long totalPage) {
        this.list = list;
        this.total = total;
        this.totalPages = totalPage;
    }

    public PageResult(List<T> list, Long total, Integer pageSize, Integer pageNo) {
        this.list = list;
        this.total = total;
        this.setPageNo(pageNo);
        this.setPageSize(pageSize);
    }

    public PageResult(Long total) {
        this.list = Collections.EMPTY_LIST;
        this.total = total;
    }

    public int getTotalPages() {
        return this.getPageSize() == 0 ? 1 : (int)Math.ceil((double)this.total / (double)this.getPageSize());
    }

    public static <T> PageResult<T> empty() {
        return new PageResult<>(0L);
    }

    public static <T> PageResult<T> empty(Long total) {
        return new PageResult<>(total);
    }

}
